0% completed
Problem Statement
Given a root node of the binary search tree, rearrange the BST in in-order such that left-most node becomes the root node of the tree, and each node has only one right child but no left child.
Examples
Example 1:
- Input: root =
[2,1,4,null,null,3]
- Expected Output:
[1,null,2,null,3,null,4] - Justification: The original tree has the root at 2, with 1 as the left child and a subtree rooted at 4 with 3 as its left child. The rearranged tree starts with 1, then proceeds to 2, followed by 3, and ends with 4, each as the right child of the previous.
Example 2:
- Input: root =
[3,2,5,1,null,4,6] - Expected Output:
[1,null,2,null,3,null,4,null,5,null,6] - Justification: Starting from 1 (the smallest value), each node is connected to the next higher value as its right child until the highest value, 6, is reached.
Example 3:
- Input: root =
[1,null,2,null,3,null,4,null,5] - Expected Output:
[1,null,2,null,3,null,4,null,5] - Justification: The input tree is already a right-skewed tree representing an ascending order sequence. Therefore, the output remains the same as the input.
Solution
To solve this problem, we'll employ an in-order traversal approach, which naturally visits the nodes of a BST in ascending order. This traversal will help us rearrange the tree by ensuring that each node we visit is placed as the right child of the previously visited node. The key here is to maintain a pointer to the last node we've rearranged so we can easily append the next node in order. This approach is effective because it leverages the sorted property of BSTs and allows us to transform the tree in-place, ensuring space efficiency.
Initially, we'll start with a dummy node as a precursor to the first node in our rearranged tree, which simplifies the process of connecting the nodes. As we perform the in-order traversal, we'll continuously update the right child of the last node to point to the current node, effectively "flattening" the tree into a single rightward path. This method is the most effective because it does not require additional space for node storage, and it directly constructs the final structure during the traversal.
Step-by-step Algorithm
-
Initialize a Dummy Node: Start by creating a dummy node. This node acts as a placeholder to simplify the process of attaching nodes in increasing order. Its right child will eventually point to the smallest element in the tree, marking the beginning of our transformed tree.
-
In-Order Traversal Setup: Prepare to perform an in-order traversal of the BST. This type of traversal visits nodes in ascending order, which aligns perfectly with our goal of rearranging the tree.
-
Traversal and Transformation:
- Visit Left: Recursively visit the left child of the current node. This step ensures that we start with the smallest element.
- Rearrange Nodes: After visiting the left child, perform the following for the current node:
- Detach the left child by setting
currentNode.leftto null. This step is crucial as we're transforming the BST into a structure where each node only has a right child. - Attach the current node to the right of the last node processed. This is done by setting
lastNode.rightto the current node. - Update
lastNodeto the current node, preparing it to attach the next node in sequence.
- Detach the left child by setting
- Visit Right: Recursively visit the right child of the current node to continue the in-order traversal.
-
Finalize the Transformation: After the traversal is complete, the right child of the dummy node points to the smallest element, and all subsequent elements are attached as right children in increasing order. Return
dummy.rightas the new root of the transformed tree.
Algorithm Walkthrough
Let's consider the input [3,2,5,1,null,4,6]
3
/ \
2 5
/ / \
1 4 6
Step-by-Step Transformation:
-
Initialize Dummy Node: Create a dummy node. This node serves as a starting point for rearranging the tree.
-
Start In-Order Traversal:
- Begin with the root node (3).
-
Visit Left Subtree of 3:
- Move to node 2.
- Visit left subtree of 2, which is node 1.
-
Process Node 1:
- Node 1 has no left child, so we start the rearrangement here.
- Detach left child (none in this case).
- Attach node 1 to the right of the dummy node.
- Update
lastNodeto node 1.
-
Back to Node 2:
- Detach left child (previously node 1).
- Attach node 2 to the right of node 1 (the current
lastNode). - Update
lastNodeto node 2.
-
Proceed to Root Node 3:
- Node 3's left child (node 2) has been fully processed.
- Detach left child (node 2 is now attached to the transformed tree, not directly as a left child).
- Attach node 3 to the right of node 2 (the current
lastNode). - Update
lastNodeto node 3.
-
Visit Right Subtree of 3:
- Move to node 5.
- Visit left subtree of 5, which is node 4.
-
Process Node 4:
- Node 4 has no left child.
- Detach left child (none).
- Attach node 4 to the right of node 3 (the current
lastNode). - Update
lastNodeto node 4.
-
Back to Node 5:
- Detach left child (node 4 is now part of the transformed tree).
- Attach node 5 to the right of node 4.
- Update
lastNodeto node 5.
-
Visit Right Subtree of 5:
- Move to node 6.
- Process node 6 similarly, attaching it to the right of node 5.
Final Transformed Tree:
The final tree, starting from dummy.right, follows a straight path: 1 -> 2 -> 3 -> 4 -> 5 -> 6.
Code
Complexity Analysis
Time Complexity
n is the number of nodes in the tree. Since we perform constant time operations for each node (reassigning pointers), the total time complexity is linear with respect to the number of nodes in the BST.
Space Complexity
h is the height of the BST.
On this page
Problem Statement
Examples
Solution
Step-by-step Algorithm
Algorithm Walkthrough
Code
Complexity Analysis